package com.um.jdy.mp.order.manager;

import com.um.jdy.common.charge.entity.po.FreeUser;
import com.um.jdy.common.charge.service.FreeUserService;
import com.um.jdy.common.device.service.StationService;
import com.um.jdy.common.member.code.UserCode;
import com.um.jdy.mp.order.entity.dto.FreeUserDTO;
import com.um.jdy.mp.order.entity.vo.FreeUserItemVO;
import com.um.jdy.mp.user.entity.vo.MemberUserVO;
import com.um.jdy.mp.user.service.SessionUserService;
import com.um.springboot.starter.code.CommonCode;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.ListParams;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Component
@Slf4j
public class FreeUserManager extends BaseManager<FreeUserService> {

    @Autowired
    StationService stationService;

    @Autowired
    SessionUserService sessionUserService;

    public Response<List<FreeUserItemVO>> list(ListParams params) {
        params.getCondition().put("f.com_id", getComId());
        params.getCondition().put("f.create_user_id", sessionUserService.getTokenUserId());
        List<FreeUserItemVO> list = service.getCMSViewModelListBySqlName("get_free_user_list", FreeUserItemVO.class,
                params.getProperties(), params.getValues(), params.getRangeEntitys(), params.getOrderNames(), params.getSortOns());
        return Response.success(list);
    }

    /**
     * 分页列表
     *
     * @param params
     * @return
     */
    public Response<Page<FreeUserItemVO>> page(PageParams params) {
        Page<FreeUserItemVO> page = new Page();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());

        params.getCondition().put("f.com_id", getComId());
        params.getCondition().put("f.create_user_id", sessionUserService.getTokenUserId());

        page = service.getCMSViewModelPageBySqlName(page, "get_free_user_page", "get_free_user_count", FreeUserItemVO.class,
                params.getProperties(), params.getValues(), params.getRangeEntitys(), params.getOrderNames(), params.getSortOns());
        return Response.success(page);
    }

    /**
     * 根据手机号搜索白名单用户
     *
     * @param mobile
     * @return
     */
    public Response<List<MemberUserVO>> getUserByMobile(String mobile) {

        if (StringUtil.isEmpty(mobile) || mobile.trim().length() != 11) {
            throw new ServiceException(UserCode.MobileFormatError);
        }
        List<MemberUserVO> list = service.getViewModelList("get_free_users_by_mobile", MemberUserVO.class,
                new Object[]{mobile,getComId()});
        return Response.success(list);
    }


    /**
     * 新增
     *
     * @param dto
     * @return
     */
    public Response add(FreeUserDTO dto) {
        FreeUser freeUser = service.getEntity(new String[]{"member_user_id", "device_station_id", "device_pile_id","create_user_id"},
                new Object[]{dto.getUserId(), dto.getStationId(), dto.getDeviceId(),sessionUserService.getTokenUserId()});
        if (freeUser == null) {
            freeUser = new FreeUser();
            freeUser.setMemberUserId(dto.getUserId());
            freeUser.setCreateUserId(sessionUserService.getTokenUserId());
            freeUser.setDeviceStationId(dto.getStationId());
            if (!StringUtil.isEmpty(dto.getDeviceId())) {
                freeUser.setDevicePileId(dto.getDeviceId());
            }
            freeUser.setType("merchant");
            freeUser.setCreateTime(new Date());
            service.save(freeUser);
        }
        return Response.success();
    }

    public Response<FreeUser> get(String id) {

        checkPermission(id);

        return Response.success(service.getEntity(id));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public Response delete(String id) {

        checkPermission(id);

        boolean result = service.delete(id);
        return result ? Response.success() : Response.failed();
    }

    /**
     * 校验数据权限
     * @param id
     */
    private void checkPermission(String id){
        FreeUser entity = service.getEntity(id);
        if(entity == null)
            throw new ServiceException(CommonCode.DataNotExist);

        if(!StringUtil.equals(sessionUserService.getTokenUserId(),entity.getCreateUserId()))
            throw new ServiceException(CommonCode.NoDataPermission);
    }
}
